<template>
	<view class="container">
		

		<view class="article-form">

			<view class="form-item">
				<input class="title-input" type="text" v-model="articleTitle" placeholder="请输入帖子标题" maxlength="50" />
				<view class="word-count">{{ articleTitle.length }}/50</view>
			</view>
			

			<view class="form-item">
				<textarea class="content-input" v-model="articleContent" placeholder="请输入帖子内容" maxlength="2000"></textarea>
				<view class="word-count">{{ articleContent.length }}/2000</view>
			</view>
			

			<view class="form-item">
				<view class="image-title">添加图片</view>
				<view class="image-list">
					<view class="image-item" v-for="(item, index) in uploadImages" :key="index">
						<image :src="item" mode="aspectFill"></image>
						<view class="delete-btn" @click="deleteImage(index)">
							<uni-icons type="closeempty" size="20" color="#fff"></uni-icons>
						</view>
					</view>
					<view class="upload-btn" @click="chooseImage" v-if="uploadImages.length < 9">
						<uni-icons type="camera" size="30" color="#999"></uni-icons>
					</view>
				</view>
			</view>
			

			<view class="form-item">
				<view class="tag-title">选择标签</view>
				<view class="tag-list">
					<view 
						class="tag-item" 
						v-for="(tag, index) in tags" 
						:key="index"
						:class="{ 'selected': tag.selected }"
						@click="toggleTag(index)"
					>
						{{ tag.name }}
					</view>
				</view>
			</view>
		</view>
		
		<!-- 底部操作栏 -->
		<view class="bottom-bar">
			<view class="publish-btn" @click="publishArticle">发布帖子</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				articleTitle: '',
				articleContent: '',
				uploadImages: [],
				tags: [
					{ name: '问题求助', selected: false },
					{ name: '经验分享', selected: false },
					{ name: '航班信息', selected: false },
					{ name: '休息室', selected: false },
					{ name: '好物推荐', selected: false },
					{ name: '机场美食', selected: false }
				]
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},
			
			// 选择图片
			chooseImage() {
				const self = this;
				uni.chooseImage({
					count: 9 - self.uploadImages.length,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success(res) {
						// 上传成功后，将图片添加到列表
						self.uploadImages = [...self.uploadImages, ...res.tempFilePaths];
					}
				});
			},
			
			// 删除图片
			deleteImage(index) {
				this.uploadImages.splice(index, 1);
			},
			
			// 切换标签选中状态
			toggleTag(index) {
				this.tags[index].selected = !this.tags[index].selected;
			},
			
			// 发布帖子
			publishArticle() {
				// 表单验证
				if (!this.articleTitle.trim()) {
					uni.showToast({
						title: '请输入帖子标题',
						icon: 'none'
					});
					return;
				}
				
				if (!this.articleContent.trim()) {
					uni.showToast({
						title: '请输入帖子内容',
						icon: 'none'
					});
					return;
				}
				
				// 获取选中的标签
				const selectedTags = this.tags
					.filter(tag => tag.selected)
					.map(tag => tag.name);
				
				// 构建帖子数据
				const articleData = {
					title: this.articleTitle,
					content: this.articleContent,
					images: this.uploadImages,
					tags: selectedTags,
					timestamp: Date.now()
				};
				
				console.log('发布帖子数据:', articleData);
				
				// 显示成功提示
				uni.showToast({
					title: '帖子发布成功',
					icon: 'success',
					success: () => {
						// 延迟返回上一页
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.container {
	background-color: #f6f7fb;
	padding-bottom: 120rpx;
}

/* 顶部导航 */
.nav-bar {
	background-color: #fff;
	padding-bottom: 10rpx;
	box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
}

.status-bar {
	height: var(--status-bar-height);
}

.nav-content {
	padding: 20rpx 30rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.back-btn {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.page-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.right-placeholder {
	width: 60rpx;
}

/* 帖子表单 */
.article-form {
	padding: 30rpx;
}

.form-item {
	background-color: #fff;
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
	position: relative;
}

.title-input {
	height: 80rpx;
	font-size: 32rpx;
	font-weight: bold;
}

.content-input {
	width: 100%;
	height: 400rpx;
	font-size: 28rpx;
	line-height: 1.6;
}

.word-count {
	font-size: 24rpx;
	color: #999;
	text-align: right;
	margin-top: 10rpx;
}

/* 图片上传 */
.image-title, .tag-title {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
}

.image-list {
	display: flex;
	flex-wrap: wrap;
	gap: 20rpx;
}

.image-item, .upload-btn {
	width: 200rpx;
	height: 200rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
	overflow: hidden;
	position: relative;
}

.image-item image {
	width: 100%;
	height: 100%;
}

.delete-btn {
	position: absolute;
	top: 6rpx;
	right: 6rpx;
	width: 40rpx;
	height: 40rpx;
	background-color: rgba(0,0,0,0.5);
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.upload-btn {
	border: 2rpx dashed #ddd;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* 标签选择 */
.tag-list {
	display: flex;
	flex-wrap: wrap;
	gap: 20rpx;
}

.tag-item {
	padding: 12rpx 24rpx;
	background-color: #f5f5f5;
	border-radius: 30rpx;
	font-size: 26rpx;
	color: #666;
	
	&.selected {
		background-color: #6c5ce7;
		color: #fff;
	}
}

/* 底部操作栏 */
.bottom-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background-color: #fff;
	padding: 20rpx 30rpx;
	box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
}

.publish-btn {
	background-color: #6c5ce7;
	color: #fff;
	font-size: 32rpx;
	font-weight: bold;
	padding: 25rpx 0;
	border-radius: 8rpx;
	text-align: center;
}
</style>
